<?php
/*
Copyright 2011 Erick Martínez Sámano

This file is part of PHP Quick Calendar E.

PHP Quick Calendar E is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PHP Quick Calendar E is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

<http://www.gnu.org/licenses/>
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );
class pqceModelsmall extends JModel{
	var $day;
	var $month;
	var $year;
	var $today;
	var $weeksInMonth;
	var $firstDay;
	var $base;
	var $daysInMonth;

	function init (&$qcbe){
		$this->day=$qcbe->day;
		$this->month=$qcbe->month;
		$this->year=$qcbe->year;
		$this->weeksInMonth=$qcbe->weeksInMonth;
		$this->today=&$qcbe->today;
		$this->firstDay=&$qcbe->firstDay;
		$this->daysInMonth=$qcbe->daysInMonth;
		$this->base=&$qcbe;
	}
	function createLinks() {
		$db =& JFactory::getDBO();
		$m=$this->month;$y=$this->year;
		$dT = "{$y}-{$m}-{$this->daysInMonth}";
		$qry = "SELECT q.refdate, q.enddate, q.type, q.active FROM #__qcalendar as q WHERE q.active='1' AND IF (q.type!=0 && q.enddate != '0000-00-00',q.refdate<=q.enddate AND (YEAR(q.enddate)>'$y' || (MONTH(q.enddate)>='$m'  && YEAR(q.enddate)='$y') ),1) AND (q.refdate <= '$dT') AND (  (q.type='0' && MONTH(q.refdate)='$m' && YEAR(q.refdate)='$y') OR (((q.type='1' && MONTH(q.refdate)='$m') || q.type='2') && (YEAR(q.enddate)!='$y' || MONTH(q.enddate)!='$m' || DAY(q.refdate)<=DAY(q.enddate))) OR (q.type='3') OR (q.type='4' && ( q.enddate='0000-00-00' || q.enddate>'$y-$m-7' || (DAYOFWEEK(q.refdate)-$this->firstDay +7)%7 <=DAY(q.enddate)) ) ) order by q.hr asc, q.min asc";
		$db->setQuery($qry);
		return $db->loadObjectList();
	}
	function createBody(&$links=array(), &$cell=array(), $view=''){
		$rn=count($links);
		$firstWDay = array();
		$v= ($view)? "&view=".$view:'';
		$compdate="{$this->year}-{$this->month}-";
		for ($r=0; $r < $rn; $r++){
			$row = &$links[$r];
			if ($row->type==4){$daySem[$r]=$this->getDay($row->refdate);$firstWDay[$r]=($daySem[$r]-$this->firstDay +7)%7;}
		}
		unset($daySem);
		for ($i=0;$i<$this->weeksInMonth;$i++) {
			for ($j=0;$j<7;$j++) {
				$val = &$cell[$i][$j]['value'];
				if (($this->today['day'] == $val) && ($this->today['month'] == $this->month) && ($this->today['year'] == $this->year)) {
					$cell[$i][$j]['isToday'] = 1;
				}
				else {
					$cell[$i][$j]['isToday'] = 0;
				}
				$cell[$i][$j]['day']=null;
				if ($val==''){continue;}
				for ($r=0; $r < $rn; $r++){
					$row = &$links[$r]; $f=explode('-',$row->refdate); $day=(int)$f[2];
					unset($f);
					if ( ($day == $val && $row->type<3) || ( ($row->type==3  || ($row->type==4 && ($val-$firstWDay[$r])%7==0)) && $this->base->leqDate($row->refdate,$compdate.$val) && $this->base->leqDate($compdate.$val,$row->enddate) ) ){ 
						$cell[$i][$j]['day']= '<a class="pQCEsmall" href="'.JRoute::_("index.php?option=com_pqce$v&f=$val.$this->month.$this->year")."\"><span class='QCEsmall'>$val</span></a>";
					break;
					}
				}
			}
		}
		return $cell;
	}
	private function getDay($d1){
		$db =& JFactory::getDBO();
		$db->setQuery("SELECT DAYOFWEEK('$d1')");
		return $db->loadResult();
	}
}
